package br.com.cast.scc.pages.veiculos;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.feedback.ExactLevelFeedbackMessageFilter;
import org.apache.wicket.feedback.FeedbackMessage;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import br.com.cast.scc.enuns.StatusRegistroAtivo;
import br.com.cast.scc.filtros.FiltroPesquisaVeiculos;
import br.com.cast.scc.model.DominioTipoVeiculo;
import br.com.cast.scc.model.Veiculo;
import br.com.cast.scc.service.TipoVeiculoService;
import br.com.cast.scc.service.VeiculoService;

public class ConsultarVeiculoIndexForm  extends Form<FiltroPesquisaVeiculos>{


	private static final long serialVersionUID = 1L;
	
	@SpringBean(name="tipoVeiculoServiceImpl")
	private TipoVeiculoService tipoVeiculoService;
	
	@SpringBean(name="veiculoServiceImpl")
	private VeiculoService veiculoService;
	
	private VeiculoGridPanel gridPanel;
	
	private WebMarkupContainer panelCamposTerrestres;
	
	private WebMarkupContainer panelCamposFluviais;

	public ConsultarVeiculoIndexForm(String id) {
		super(id);
		
		add(new FeedbackPanel("msgErro", 
				new ExactLevelFeedbackMessageFilter(
					FeedbackMessage.ERROR)).setOutputMarkupId(true));

			add(new FeedbackPanel("msgSucesso",
				new ExactLevelFeedbackMessageFilter(
					FeedbackMessage.SUCCESS)));

		criaLinkIncluir();
		
		panelCamposTerrestres = new WebMarkupContainer("panelCamposTerrestres");
		panelCamposTerrestres.setVisible(false);
		panelCamposTerrestres.setOutputMarkupPlaceholderTag(true);

		panelCamposFluviais = new WebMarkupContainer("panelCamposFluviais");
		panelCamposFluviais.setVisible(false);
		panelCamposFluviais.setOutputMarkupPlaceholderTag(true);
		
		FiltroPesquisaVeiculos filtro = new FiltroPesquisaVeiculos(veiculoService);
		
		setDefaultModel(new CompoundPropertyModel<FiltroPesquisaVeiculos>(filtro));
		
		
		IChoiceRenderer<DominioTipoVeiculo> renderer = new ChoiceRenderer<>("descricao");	
		
		final DropDownChoice<DominioTipoVeiculo> tipoVeiculo = new DropDownChoice<DominioTipoVeiculo>(
				"tipoVeiculo", tipoVeiculoService.consultaTodos(), renderer);
		
		tipoVeiculo.add(new AjaxFormComponentUpdatingBehavior("change") {
			

			private static final long serialVersionUID = 1L;

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				
				DominioTipoVeiculo tipo = tipoVeiculo.getModelObject();
				
				boolean isTerrestre = tipo.getCodigo()
					.equals(DominioTipoVeiculo.COD_TERRESTRE);
				
				panelCamposTerrestres.setVisible(isTerrestre);
				panelCamposFluviais.setVisible(!isTerrestre);
				
				target.add(panelCamposTerrestres
						, panelCamposFluviais);
				
			}
		});
		
		add(tipoVeiculo);
		
		panelCamposFluviais.add(new TextField<>("nuEmbarcacao"));
		
		panelCamposTerrestres.add(new TextField<>("placa"));
		
		panelCamposTerrestres.add(new TextField<>("rntrc"));
		
		IChoiceRenderer<StatusRegistroAtivo> rendererStatus =
				new ChoiceRenderer<>("descricao");
				
				add(new RadioChoice<>("status", 
						StatusRegistroAtivo.valores(), 
						rendererStatus));
					
					gridPanel = new VeiculoGridPanel("panelVeiculos", filtro);
					add(gridPanel);
					
					add(new AjaxButton("pesquisar") {

						private static final long serialVersionUID = 1L;

						@Override
						protected void onSubmit(AjaxRequestTarget target, 
								Form<?> form) {
							target.add(gridPanel);
						}
						
					});
					
					add(panelCamposTerrestres
					  , panelCamposFluviais);

	}

	private void criaLinkIncluir() {
		add(new Link("incluir"){


			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(CadastrarVeiculoPage.class);
			}
			
		});
	}

}
